package com.iemes.controller.device;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DeviceMaintainFormMap;
import com.iemes.entity.DeviceTypeFormMap;
import com.iemes.mapper.DeviceMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/devicemaintain/")
public class DeviceMaintainController extends BaseController {

    @Inject
    private UserMapper userMapper;

    @Inject
    private DeviceMapper deviceMapper;


    @RequestMapping("devicemaintain")
    public String listUI(Model model) throws Exception {
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>","<button type=\"button\" id=\"verify\" class=\"btn btn-danger marR10\">验证</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/device/maintain/maintainlist";
    }

    @ResponseBody
    @RequestMapping("maintain_findByPage")
//    public PageView findByPage(String pageNow,
    public PageInfo findByPage(String pageNow,
                               String pageSize, HttpServletRequest request) throws Exception {
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
//        String name =  getPara("deviceName");

        DeviceMaintainFormMap deviceMaintainFormMap=getFormMap(DeviceMaintainFormMap.class);
        deviceMaintainFormMap = toFormMap(deviceMaintainFormMap,pageNow,pageSize,deviceMaintainFormMap.getStr("orderby"));
        String name=request.getParameter("deviceMaintainEmp");
        System.out.println("name:..."+name);

        if(name==null || name.equals("")){
            List<DeviceMaintainFormMap> deviceMaintainFormMaps= deviceMapper.getDeviceMaintainInfo();
//            pageView.setRecords(deviceMaintainFormMaps);
            //            return pageView;
            PageInfo pageInfo = new PageInfo<>(deviceMaintainFormMaps);
            return pageInfo;

        }else {
            List<DeviceMaintainFormMap> deviceMaintainFormMaps=deviceMapper.getDeviceMaintainInfoByName(name);
//            pageView.setRecords(deviceMaintainFormMaps);
//            return pageView;
            PageInfo pageInfo = new PageInfo<>(deviceMaintainFormMaps);
            return pageInfo;

        }

    }

    //获取json数据
    @ResponseBody
    @RequestMapping("getType")
    public String getType(HttpServletRequest request){
        //获取type信息
//        DeviceTypeFormMap deviceTypeFormMap = deviceMapper.findbyFrist("deviceTypeId", getPara("id"), DeviceTypeFormMap.class);
        String code = request.getParameter("id");
        System.out.println("code:"+code);
        DeviceTypeFormMap deviceTypeFormMap = deviceMapper.getTypeSpecByCode(code);
        return JSONObject.toJSONString(deviceTypeFormMap.get("deviceTypeSpec"));
    }

    @RequestMapping("maintain_addUI")
    public String addUI(Model model) throws Exception {
        model.addAttribute("emp", getEmp());
//        model.addAttribute("devType", deviceMapper.getDeviceType());
        model.addAttribute("device",deviceMapper.getDeviceInfo());
        return Common.BACKGROUND_PATH + "/device/maintain/maintainadd";
    }

    //verify
    @RequestMapping("maintain_verifyUI")
    public String verifyUI(Model model) throws Exception{
        model.addAttribute("emp", getEmp());
//        model.addAttribute("devType", deviceMapper.getDeviceType());
        model.addAttribute("device",deviceMapper.getDeviceInfo());

        String id=getPara("id");
        if(Common.isNotEmpty(id)){
//            model.addAttribute("devType", deviceMapper.getDeviceType());
            model.addAttribute("user",userMapper.findbyFrist("deviceMaintainId", id, DeviceMaintainFormMap.class));
        }

        return Common.BACKGROUND_PATH+"/device/maintain/maintainverify";
    }

    //更新状态
    @ResponseBody
    @RequestMapping("maintain_verifyUp")
    public String verifyUpdate(){
        //更新数据
        String id = getPara("deviceMaintainId");//更新id
        String state = getPara("deviceMaintainstate");//更新状态
        String result = getPara("deviceMaintainResult");//故障处理
        System.out.println("......"+id+state+result);
        if(deviceMapper.upDevice(id, state, result) == 1){
            return "success";
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("maintain_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备维修管理",methods="设备维修新增")//凡需要处理业务逻辑的.都需要记录操作日志
    public String addEntity()  {
        DeviceMaintainFormMap deviceMaintainFormMap=getFormMap(DeviceMaintainFormMap.class);

        System.out.println("deviceMaintainFormMap...:"+deviceMaintainFormMap);
        try {
            Date date= new Date();
            SimpleDateFormat dateFormat_min=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            deviceMaintainFormMap.set("deviceMaintainDate",dateFormat_min.format(date));
            userMapper.addEntity(deviceMaintainFormMap);//新增后返回新增信息
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }


    @RequestMapping("maintain_editUI")
    public String editUI(Model model) throws Exception{

        model.addAttribute("emp", getEmp());
//        model.addAttribute("devType", deviceMapper.getDeviceType());
        model.addAttribute("device",deviceMapper.getDeviceInfo());

        String id=getPara("id");

        if(Common.isNotEmpty(id)){
//            model.addAttribute("devType", deviceMapper.getDeviceType());
            model.addAttribute("user",userMapper.findbyFrist("deviceMaintainId", id, DeviceMaintainFormMap.class));
        }
        return Common.BACKGROUND_PATH+"/device/maintain/maintainedit";
    }

    @ResponseBody
    @RequestMapping("maintain_editEntity")
    @SystemLog(module="设备维修管理",methods="设备维修编辑")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity() throws Exception {
        DeviceMaintainFormMap deviceMaintainFormMap = getFormMap(DeviceMaintainFormMap.class);
//        userMapper.deleteByAttribute("deviceMaintainId", deviceMaintainFormMap.get("deviceMaintainId")+"", DeviceMaintainFormMap.class);
        Date date= new Date();
        SimpleDateFormat dateFormat_min=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        deviceMaintainFormMap.set("deviceMaintainDate",dateFormat_min.format(date));
        userMapper.editEntity(deviceMaintainFormMap);
//        userMapper.addEntity(deviceMaintainFormMap);
        return "success";
    }



    @ResponseBody
    @RequestMapping("maintain_deleteEntity")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备故障管理",methods="删除设备故障")//凡需要处理业务逻辑的.都需要记录操作日志
    public String deleteEntity() throws Exception {
        String[] ids = getParaValues("ids");
        System.out.println("ids..."+ids);
        for (String id : ids) {
            baseMapper.deleteByAttribute("deviceMaintainId", id, DeviceMaintainFormMap.class);
        }
        return "success";
    }


}
